﻿using System.Collections.Generic;
using System.Text;
using UnityEngine;

namespace GameBoxTools
{
    public static class HierarchyHelper
    {
        /// <summary>
        /// 获取transform所在的层级路径
        /// </summary>
        /// <param name="transform"></param>
        /// <returns></returns>
        public static string GetHierarchyPath(this Transform transform)
        {
            if (transform == null)
                return null;

            var sb = new StringBuilder();

            var stack = new Stack<Transform>();

            do
            {
                stack.Push(transform);
                transform = transform.parent;

            } while (transform);

            sb.Append(stack.Pop().name);

            while (stack.Count > 0)
            {
                sb.Append("/");
                sb.Append(stack.Pop().name);
            }

            return sb.ToString();
        }
    }
}
